package com.polukeuro.app.share.request;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.polukeuro.app.client.events.ResponseErrorEvent;
import com.polukeuro.app.client.events.ResponseEvent;
import com.polukeuro.app.client.manager.EventManager;

public class GenericRequestCallback implements RequestCallback {
	
	public void onError(Request request, Throwable exception) {
		System.err.println("Request: " + request.toString() + " e:" + exception.fillInStackTrace());
		EventManager.getInstance().publish(new ResponseErrorEvent(exception));
	}

	public void onResponseReceived(Request request, Response response) {
		if (200 == response.getStatusCode()) {
			System.out.println("Response: " + response.getStatusCode());
			EventManager.getInstance().publish(new ResponseEvent(response.getText()));
		} else {
			System.err.println("Response: " + response.getStatusCode());
			System.err.println("Cause: " + response.getStatusText());
			EventManager.getInstance().publish(new ResponseErrorEvent(response.getText()));
		}
	}
}
